//
//  pinglunVC.m
//  K-Music
//
//  Created by 林杰良 on 16/1/8.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "pinglunVC.h"
#import "UIImageView+WebCache.h"

@interface pinglunVC ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView*PLTableView;

    
    NSMutableArray*pinglunData;
}

@end

@implementation pinglunVC


-(void)backTheFirstPage{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createrleftBTN{
    UIButton*leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 20, 10, 15);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"top_menu_left_back_ico_1.png"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backTheFirstPage) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem=leftItem;
    
    UILabel *MenuText = [[UILabel alloc] initWithFrame:CGRectMake(130, 0, 120, 50)];
    
    MenuText.backgroundColor = [UIColor clearColor];
    
    MenuText.textColor=[UIColor blackColor];
    
    MenuText.textAlignment=NSTextAlignmentCenter;
    
    [MenuText setFont:[UIFont systemFontOfSize:17.0f]];
    
    [MenuText setText:@"评论"];
    
    self.navigationItem.titleView=MenuText;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createrleftBTN];
    self.view.backgroundColor=[UIColor whiteColor];
    
    [self creatUI];
    [self downloadTheGuiBinData];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark---pinglun{

-(void)downloadTheGuiBinData{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    //获取所有的贵宾信息
    NSString *intactURL = [NSString stringWithFormat:@"http://119.29.113.233:8080/api/comments"];
    
    //根据id获取信息
    
    //    NSString *intactURL = [NSString stringWithFormat:@"http://119.29.113.233:8080/api/comments/user/%@",userId];
    
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary*dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"成功评论GET --> %@", dic);
        
        pinglunData=[NSMutableArray array];
        
        NSLog(@"resultCode:%@",dic[@"resultCode"]);
        NSArray*content=dic[@"body"][@"content"];
        
        for (NSDictionary *guibin_Datas in content) {
            PingLun*aa=[[PingLun alloc]init];
            
            aa.aid=guibin_Datas[@"id"];
            aa.userId=guibin_Datas[@"userId"];
            aa.recordId=guibin_Datas[@"recordId"];
            aa.content=guibin_Datas[@"content"];
            aa.createdBy=guibin_Datas[@"createdBy"];
            aa.createdDate=guibin_Datas[@"createdDate"];
            aa.lastModifiedBy=guibin_Datas[@"lastModifiedBy"];
            aa.lastModifiedDate=guibin_Datas[@"lastModifiedDate"];
            [aa fixedString];
            [pinglunData addObject:aa];
            
        }
        
        [PLTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        
        
    }];
    
}

-(void)creatUI{
    
    PLTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-50)];
    PLTableView.delegate=self;
    PLTableView.dataSource=self;
    PLTableView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"tableview"]];
    [self.view addSubview: PLTableView];
    
    UIView*textview=[[UIView alloc]init];
    textview.backgroundColor=[UIColor yellowColor];
    textview.frame=CGRectMake(0, PLTableView.frame.origin.y+PLTableView.frame.size.height, WIDTH, 50) ;
    [self.view addSubview:textview];
    
    UITextField*tt=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    tt.backgroundColor=[UIColor whiteColor];
    tt.text =@"输入评论";
    
    [textview addSubview:tt];
    

    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString*inf=@"pinglunCell";
    
    pinglunCell*cell=[tableView  dequeueReusableCellWithIdentifier:inf];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"pinglunCell" owner:self options:nil ] firstObject];
        
    }
    PingLun*pl=[[PingLun alloc]init];
    pl=pinglunData[indexPath.row];
//    [cell.imgStr sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:nil]] placeholderImage:nil];
    cell.nameStr.text=[NSString stringWithFormat:@"%@",pl.userId];
    cell.infoStr.text=[NSString stringWithFormat:@"%@",pl.content];
    cell.timeStr.text=[NSString stringWithFormat:@"%@",pl.createdDate];
    
    cell.backgroundColor=[UIColor clearColor];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
